# -*- coding: utf-8 -*-
import os

class DeviceProperty(object):
    index = 0
    def __init__(self):
        self._clazz = None
        self._Icon = ""
        self._ToolTip = ""
        self._name = ''
        self._MenuText = ""
        self._MenuItem: list = []

    def appendMenu(self, name, func):
        self._MenuItem.append((name, func))

    def getMenuItem(self):
        return self._MenuItem

    def clearMenuItem(self):
        self._MenuItem.clear()

    @property
    def MenuText(self):
        return self._MenuText

    @MenuText.setter
    def MenuText(self, value):
        self._MenuText = value

    @MenuText.deleter
    def MenuText(self):
        self._MenuText = ""

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

    @name.deleter
    def name(self):
        self._name = None

    @property
    def clazz(self):
        return self._clazz

    @clazz.setter
    def clazz(self, value):
        self._clazz = value

    @clazz.deleter
    def clazz(self):
        self._clazz = None

    @property
    def Icon(self):
        return self._Icon

    @Icon.setter
    def Icon(self, value):
        self._Icon = value

    @Icon.deleter
    def Icon(self):
        self._Icon = ""

    @property
    def ToolTip(self):
        return self._ToolTip

    @ToolTip.setter
    def ToolTip(self, value):
        self._ToolTip = value

    @ToolTip.deleter
    def ToolTip(self):
        self._ToolTip = ""

class DeviceManager(object):
    def __init__(self, sourcepath):
        self.sourcepath = sourcepath
        self.devices: list = []
        self.loadDevices()

    def getDevices(self):
        return self.devices

    def getDeviceHandleByName(self, name):
        for dev in self.devices:
            if dev.name == name:
                return dev.clazz
        return None

    def loadDevices(self):
        devicepath = os.path.join(self.sourcepath, "device")
        for devicename in os.listdir("device"):
            path = os.path.join(devicepath, devicename)
            if os.path.isdir(path):
                for fn in os.listdir(path):
                    if fn == "init.py":
                        pluginName = os.path.splitext(fn)[0]
                        __import__("device." + devicename + "." + pluginName, fromlist=[pluginName])
                    elif fn == "initGui.py":
                        module = DeviceProperty()
                        module.name = devicename
                        pluginName = os.path.splitext(fn)[0]
                        plugin = __import__("device." + devicename + "." + pluginName, fromlist=[pluginName])
                        clazz = plugin.getModuleClass()
                        o = clazz()
                        module.clazz = o
                        module.Icon = o.Icon
                        module.ToolTip = o.ToolTip
                        module.MenuText = o.MenuText
                        self.devices.append(module)
            elif os.path.isfile(devicename):
                print("is file")
            else:
                print("non")
            # if not filename.endswith(".py") or filename.startswith("_"):
            #     continue
            # self.runPlugin(filename)